#include "getsetfield.h"

GetSetField::GetSetField(QObject *parent) :
    QObject(parent)
{
}

QByteArray GetSetField::getSN(QByteArray packet)
{
    if(!packet.toUpper().contains("SN:"))
        return "-1";
    else
    {
        qint32 posOfSN=packet.indexOf("SN:");
        qint32 posOfNewLine=packet.indexOf('\n',posOfSN);
        return packet.mid(posOfSN+3,posOfNewLine-posOfSN-2-1);
    }
}

QByteArray GetSetField::setSN(QByteArray packet, QByteArray baSN)
{
    if(!packet.toUpper().contains("SN:"))
    {
        if(!packet.contains('\n'))
            packet.append("\nSN:"+baSN);
        else
        {
            packet.insert(packet.indexOf('\n')+1,"SN:"+baSN+'\n');
        }
    }
    else
    {
        qint32 posOfSN=packet.toUpper().indexOf("SN:");
        qint32 posOfNewLine=packet.indexOf('\n',posOfSN);
        packet.replace(posOfSN+3,posOfNewLine-posOfSN-2-1,baSN);
    }
    return packet;
}
